﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using RuleCode.Table;

namespace chatblog.login
{
    public partial class login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                setvisible();
            }
            if (Request.Url.LocalPath.ToLower() == "/commonchat/commonchatroom.aspx")
            {
                this.Button_loginout.Text = "退出";
            }
            else
            {
                this.Button_loginout.Text = "注销";
            }
        }
        private void setvisible()
        { 
                if (AliApi.appUserId != "")
                {
                    Label_id.Text = AliApi.appUserId; 
                    Panel_haslogin.Visible = true;
                    Panel_unlogin.Visible = false;
                }
                else
                {
                    Panel_haslogin.Visible = false;
                    Panel_unlogin.Visible = true;
                }        
        }

        protected void Button_loginin_Click(object sender, EventArgs e)
        {
            
            if (cbuser.login(TextBox1.Text,
                        TextBox2.Text) == false)
            {
                MessageBox.Show(this.Page, "用户名或者密码错误.");
                return;
            }
            cbuser user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
            if (user.Count > 0)
            {
                //user.isonline.Value = RuleCode.Enum.BooleanEnum.Yes;
                if (Request.Url.LocalPath == "/usercate/chatroom.aspx")
                {
                    user.chatIsOnLine.Value = RuleCode.Enum.BooleanEnum.Yes;
                }
                user.Modify();
                Util.isFlush = true;
            }
            setvisible();
            Response.Redirect(this.Request.Url.AbsoluteUri);
        }

        protected void Button_loginout_Click(object sender, EventArgs e)
        {
            if (Request.Url.LocalPath.ToLower() == "/commonchat/commonchatroom.aspx")
            {
                CommonChat.CommonChatRoom chatroom = (this.Page as CommonChat.CommonChatRoom);
                if (chatroom != null)
                {
                    chatroom.LoginOut(chatroom.CurrentRoomID);
                    string scriptStr = "{window.opener =null;window.open('','_self','');window.close(); }";
                    //bool isFF = Request.UserAgent.ToLower().Contains("firefox");
                    //if (isFF)
                    //    scriptStr = "document.getElementById('ctl00_ctl00_login1_Button_loginout').click();" + scriptStr;
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "hasLogin", "<script type=\"text/javascript\">" + scriptStr + "</script>", false);

                    return;
                }
            }

            if (Util.useLoginOut)
            {
                cbuser user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
                if (user.Count > 0)
                {
                    //user.isonline.Value = RuleCode.Enum.BooleanEnum.No;
                    if (Request.Url.LocalPath.ToLower () == "/usercate/chatroom.aspx")
                    {
                        user.chatIsOnLine.Value = RuleCode.Enum.BooleanEnum.No;
                        user.cbChatAddress.Value = "";
                    }
                    user.Modify();
                }
            }
            

            this.Session.Clear();
            if (Request.Cookies["laoyougushi"] != null)
            {
                HttpCookie acookie = Request.Cookies["laoyougushi"];
                //acookie.Expires = DateTime.Now.AddDays(-1);
                //Response.Cookies.Add(acookie);

                acookie.Expires = DateTime.Now.AddDays(-10);
                acookie.Values.Clear();
                //System.Web.HttpContext.Current.Response.Cookies.Set(acookie);
            }

           
            setvisible();
            Response.Redirect(this.Request.Url.AbsoluteUri);
        }


    }
}